//
//  STEmoticonPageViewCell.swift
//  STVVeibo
//
//  Created by leergou on 16/7/30.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

class STEmoticonPageViewCell: UICollectionViewCell {
    
    // 定义一个按钮数组,保存每页上的表情button
    var emoticonButtons: [STEmoticonButton] = [STEmoticonButton]()
    
//    var indexPath: NSIndexPath?{
//        didSet{
//            
//            contentLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)行"
//        }
//    }
    
    // 给 cell 定义一个装有表情模型的数组
    var emoticons: [STEmoticon]?{
        didSet{
            
            // 进来首先隐藏所有按钮
            for button in emoticonButtons{
                button.hidden = true
            }
            // 遍历一维护表情数组
            for (index, emoticon) in emoticons!.enumerate(){
                // 取出数组中按钮
                let button = emoticonButtons[index]
                // 给按钮表情模型属性赋值
                button.emoticon = emoticon
                // 显示按钮
                button.hidden = false
                // 判断是哪种表情
                if emoticon.isSTEmoji {
                    // 是emoji 表情,设置title,同时设置图片为 nil(cell 重用问题)
                    button.setTitle((emoticon.code ?? "" as String).emoji(), forState: UIControlState.Normal)
                    // 设置图片为 nil
                    button.setImage(nil, forState: UIControlState.Normal)
                } else {
                    // 是图片表情,设置 image,同时设置 title 为nil(cell 重用)
                    let image = UIImage.getEmoticonImage(emoticon.path)
                    button.setImage(image, forState: UIControlState.Normal)
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 删除按钮点击方法
    func deleteButtonClick(){
//        print("删除按钮点击了")
        // 点击删除按钮的时候,发送通知,让 textView 删除
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteButtonClick, object: nil)
    }
    
    //MARK: 表情按钮点击方法
    func emoticonButtonClick(sender: STEmoticonButton){
//        print("表情按钮点击了")
        // 发送通知,将按钮的 表情模型属性 传出去
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonsClick, object: sender.emoticon)
    }
    
    //MARK: setupUI
    func setupUI(){
        // 添加子控件
//        contentView.addSubview(contentLabel)
        addEmoticonButton()
        contentView.addSubview(deleteButton)
        // 约束
//        contentLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
    }
    
    //MAKR: 布局表情按钮
    override func layoutSubviews() {
        super.layoutSubviews()
        // 设置按钮size
        let emoticonButtonWidth = (self.frame.width - 10) / CGFloat(emoticonMaxCol)
        let emoticonButtonHeight = (self.frame.height - 20) / CGFloat(emoticonMaxRow)
        // 遍历按钮数组,设置frame
        for (index, button) in emoticonButtons.enumerate(){
            // 计算 col 和 row
            let row = CGFloat(index / emoticonMaxCol)
            let col = CGFloat(index % emoticonMaxCol)
            button.frame = CGRect(x: 5 + emoticonButtonWidth * col, y: emoticonButtonHeight * row, width: emoticonButtonWidth, height: emoticonButtonHeight)
        }
        // 设置 删除按钮的 frame
        deleteButton.frame = CGRect(x: self.frame.width - emoticonButtonWidth, y: emoticonButtonHeight * 2 , width: emoticonButtonWidth, height: emoticonButtonHeight)
    }
    
    //MARK: - 定义一个方法添加表情按钮
    func addEmoticonButton(){
        // 每页上按钮个数 等于 emoticonMaxCount 每页上的最大表情个数
        for _ in 0..<emoticonMaxCount{
            let button = STEmoticonButton(title: nil, titleColro: nil, font: 30, target: self, action: "emoticonButtonClick:")
//            button.backgroundColor = RandomColor()
            contentView.addSubview(button)
            emoticonButtons.append(button)
        }
    }
    
    //MARK: lazy
    
//    private lazy var contentLabel: UILabel = {
//        let lab = UILabel(font: 20, textColor: UIColor.redColor(), maxWidth: 60)
//        return lab
//    }()
    
    // 删除按钮
    private lazy var deleteButton: UIButton = {
       let button = UIButton(backgNorImg: nil, backghighlighted: nil, norImg: "compose_emotion_delete", highlightedImg: "compose_emotion_delete_highlighted", target: self, action: "deleteButtonClick")
        return button
    }()
}
























